/**
 * Copyright (c) 2012, Hakans Pirates - 
 * Johan Andersson spaiki17@gmail.com, 
 * Mattias Lundberg lundberg.mattias@gmail.com, 
 * Samuel Sjodin sjoodin@gmail.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the <organization> nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *  
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package pirates.inputDevices;

import com.jme3.bullet.PhysicsSpace;
import com.jme3.cinematic.MotionPath;
import com.jme3.cinematic.MotionPathListener;
import com.jme3.cinematic.events.MotionTrack;
import com.jme3.material.Material;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;

import pirates.PiratesServer;
import pirates.Settings;
import pirates.Settings.EntityTeam;
import pirates.notifications.Notify;
import pirates.ship.ShipControl;

public class AutomaticControl extends AbstractControl {

	private ShipControl shipControl;
	private MotionPath path;
	private MotionTrack motionControl;
	private boolean active = true;
	private boolean playing = false;
	private PiratesServer app;
	private static Spatial ship = null;
	private Node rootNode;
	private PhysicsSpace physics;
	private static Material black;

	public AutomaticControl(ShipControl shipControl, PiratesServer app,
			Spatial ship) {
		this.shipControl = shipControl;
		this.app = app;
		this.ship = ship;
		initPath();
	}

	void initPath() {
		Notify.debug("initializing automatic control for tradeship "
				+ ship.getName());
		path = new MotionPath();
		path.addWayPoint(ship.getWorldTranslation());
		path.addWayPoint(ship.getWorldTranslation().add(
				new Vector3f(10000, 0, 0)));// .addLocal(Settings.BrittishSpawnPoint));
		path.addWayPoint(ship.getWorldTranslation().add(
				new Vector3f(20000, 0, 40000)));

		// enables debugging
		// path.enableDebugShape(app.getAssetManager(),
		// (Node)app.getRootNode().getChild("shipNode"));

		motionControl = new MotionTrack(ship, path);
		motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation);
		motionControl.setRotation(new Quaternion().fromAngleNormalAxis(
				-FastMath.HALF_PI, Vector3f.UNIT_Y));
		motionControl.setInitialDuration(1000f);
		// motionControl.setSpeed(200f); //for debugging animation only!
		motionControl.setSpeed(2f); // TODO: for end game
		path.addListener(new MotionPathListener() {

			public void onWayPointReach(MotionTrack control, int wayPointIndex) {
				if (path.getNbWayPoints() == wayPointIndex + 1
						&& !Settings.SCENARIO_HAS_ENDED) {
					Notify.debug("Tradeship " + ship.getName()
							+ " arrived!");
					Notify.debug("position: "
							+ spatial.getWorldTranslation());
					Notify.debug("waypoint index: " + wayPointIndex);
					app.stopScenarioAndDeclareWinner(EntityTeam.BRITISH);
				} else if ((wayPointIndex + 1) < path.getNbWayPoints()) {
					// Reached way point
					Notify.debug("Tradeship " + ship.getName()
							+ " reached way point " + wayPointIndex
							+ ". Next way point: "
							+ path.getWayPoint(wayPointIndex + 1));
				}

			}
		});
		// this.go();
	}

	/**
	 * updates the ship's position based on
	 * 
	 * @return
	 */
	void onUpdate() {
	}

	public void go() {
		Notify.debug("animation started for tradeship " + ship.getName());
		motionControl.play();
	}

	public void stop() {
		motionControl.stop();
		// playing = true;
	}

	@Override
	public com.jme3.scene.control.Control cloneForSpatial(Spatial arg0) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	protected void controlRender(RenderManager arg0, ViewPort arg1) {
		// TODO Auto-generated method stub
	}

	@Override
	protected void controlUpdate(float arg0) {
		// TODO Auto-generated method stub
	}
}
